package scau.zxck.view.controller;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.CheckBoxTreeCell;
import javafx.stage.DirectoryChooser;
import scau.zxck.entity.TableInfo;
import scau.zxck.service.CreatorService;
import scau.zxck.utils.ViewUtil;
import scau.zxck.view.Main;
import scau.zxck.view.node.MyCheckBoxTreeItem;
import scau.zxck.view.stage.AlertStage;
import java.io.File;
import java.util.Iterator;

/**
 * Created by chenyulin on 2016/11/29.
 */
public class TableTreeController {

    @FXML private TreeView tableTree;
    @FXML private Button btCreate;
    @FXML private TextField tfOutDir;

    public void initialize() {
        tfOutDir.setText(Main.defaultOutDir);
        tableTree.setFocusTraversable(false);
        tfOutDir.setFocusTraversable(false);
        if(Main.tableList.size() != 0) {
            tableTree.setCellFactory(CheckBoxTreeCell.<String>forTreeView());
            btCreate.setDisable(false);
        } else
            btCreate.setDisable(true);
        tableTree.setRoot(ViewUtil.buildTableTreeRootItem());
    }

    @FXML
    public void selectAction() {
        DirectoryChooser directoryChooser = new DirectoryChooser();
        directoryChooser.setTitle("Select Output Directory");
        File file = directoryChooser.showDialog(null);
        if (file != null) {
            String path = file.getPath();
            tfOutDir.setText(path);
        }
    }
    @FXML
    public void previousAction() {
        Main.mainController.initialize();
    }
    @FXML
    public void createAction() {
        Iterator tableIterator = tableTree.getRoot().getChildren().iterator();
        boolean isEmpty = true;
        while (tableIterator.hasNext()){
            MyCheckBoxTreeItem tableItem = (MyCheckBoxTreeItem) tableIterator.next();
            Iterator ssmFileItemIterator = tableItem.getChildren().iterator();
            while (ssmFileItemIterator.hasNext()){
                MyCheckBoxTreeItem ssmFileItem = (MyCheckBoxTreeItem) ssmFileItemIterator.next();
                if(ssmFileItem.isSelected()) {
                    isEmpty = false;
                    String outDir = tfOutDir.getText();
                    if (outDir.equals("")) {
                        new AlertStage("Please choose an output directory").show();
                        return;
                    }
                    else {
                        Main.defaultOutDir = outDir;
                        TableInfo tableInfo = Main.tableList.get(ssmFileItem.getTableIndex());
                        int indexOfSsmFile = ssmFileItem.getSsmFileIndex();
                        CreatorService creatorService = new CreatorService(outDir, tableInfo);
                        switch (indexOfSsmFile) {
                            case 0 : creatorService.createEntity();break;
                            case 1 : creatorService.createDao();break;
                            case 2 : creatorService.createServiceInterface();break;
                            case 3 : creatorService.createServiceImpl();break;
                            case 4 : creatorService.createAction();break;
                            case 5 : creatorService.createMapper();
                        }
                        ViewUtil.writeInfoToConfigFile();
                    }
                }
            }
        }
        if (isEmpty)
            new AlertStage("No table selected").show();
        else
            new AlertStage("Successfully created").show();
    }

}
